package com.ruoyi.park22.service.Impl;
import com.ruoyi.CheryEcos.service.impl.EcosServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.park2.domain.Park2;
import com.ruoyi.park2.service.IPark2Service;
import com.ruoyi.park22.domain.Park22;
import com.ruoyi.park22.mapper.Park22Mapper;
import com.ruoyi.park22.service.IPark22Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.util.List;

@Service
public class Park22ServiceImpl implements IPark22Service {

    private static final Logger log = LoggerFactory.getLogger(EcosServiceImpl.class);

    @Autowired
    private IPark2Service iPark2Service;

    @Autowired
    private Park22Mapper park22Mapper;
    @Autowired
    protected Validator validator;
    @Override
    public List<Park22> selectPark22List(Park22 park22) {
        return park22Mapper.selectPark22List(park22);
    }
    @Override public Park22 selectPark22ById(Long id) {
     return park22Mapper.selectPark22ById(id);
   }
   @Override
   public int insertPark22(Park22 park22) {
     Park2 park2=iPark2Service.selectPark2ByName(park22.getName());
     park22.setStatus(park2.getStatus());
     park22.setCreateBy(park2.getCreateBy());
     park22.setRegion(park2.getRegion());
        return park22Mapper.insertPark22(park22);
   }
   @Override
   public int updatePark22(Park22 park22) {
       Park2 park2=iPark2Service.selectPark2ByName(park22.getName());
       park22.setStatus(park2.getStatus());
       park22.setCreateBy(park2.getCreateBy());
       return park22Mapper.updatePark22(park22);
   }
   @Override
   public int deletePark22ById(Long id) {
     return park22Mapper.deletePark22ById(id);
   }

   @Override
   public int deletePark22ByIds(Long[] ids) {
     return park22Mapper.deletePark22ByIds(ids);
   }
   @Override
   public List<Park22> selectPark22ByIds(Long[] ids) {
     return park22Mapper.selectPark22ByIds(ids);
   }

    @Override
    public Park22 selectPark22ByName(String region) {
        return park22Mapper.selectPark22ByName(region);
    }

    @Override
   public String importPark22(List<Park22> Park22List, Boolean isUpdateSupport) {
     if (StringUtils.isNull(Park22List) || Park22List.size() == 0) {
       throw new ServiceException("导入数据不能为空！");
     }
     int successNum = 0;
     int failureNum = 0;
     StringBuilder successMsg = new StringBuilder();
     StringBuilder failureMsg = new StringBuilder();
     for (Park22 ecos : Park22List) {
       try {
         park22Mapper.insertPark22(ecos);
         successNum++;
         successMsg.append("<br/>" + successNum + "、数据 " + " 导入成功");
       } catch (Exception e) {
         failureNum++;
         String msg = "<br/>" + failureNum + "、数据 " + " 导入失败：";
         failureMsg.append(msg + e.getMessage());
         log.error(msg, e);
       }
     }
     if (failureNum > 0) {
       failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
       throw new ServiceException(failureMsg.toString());
     } else {
       successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
     }
     return successMsg.toString();
   }
}
